<template>
  <div id="fish_contanier">
    <router-view/>
    <div class="hiddenaudio">
      <audio :src="src" ref="myaudio" preload="auto" hidden></audio>
    </div>
  </div>
</template>

<script>
  import '@/assets/css/base.css';
  // import '@/assets/js/e_fish.js';
  const wx = require("weixin-js-sdk");
  import {
    mapActions
  } from "vuex";
  import axios from 'axios';

  export default {
    name: 'App',
    data() {
      return {
        code: '',
        src: 'https://zhiyangzhuli.oss-cn-beijing.aliyuncs.com/video/bgm.mp3'
      }
    },
    created() {
      this.code = this.getQueryString('code');
      this.goGet()
    },
    mounted() {
      const self = this
      this.$nextTick(() => {
        this.getShareInfo({
          url: location.href.split('#')[0],
          callback: res => {
            wx.config({
              debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
              appId: res.data.appId, // 必填，公众号的唯一标识
              timestamp: res.data.timestamp, // 必填，生成签名的时间戳
              nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
              signature: res.data.signature, // 必填，签名
              jsApiList: [
                "updateAppMessageShareData",
                "updateTimelineShareData",
                "onMenuShareAppMessage",
                "onMenuShareTimeline",
              ]
            });
            wx.ready(() => {
              const option = {
                title: '凯翼号专列送川籍农民工回家啦！',
                desc: '“凯翼号”公益专列，今年春节送您回家~',
                link: 'http://zhiyang.yizehd.com/qiangpiao/',
                imgUrl: 'http://zhiyang.yizehd.com/static/shareimg/qiangpiao.jpg',
              }
              // 微信分享给朋友
              wx.updateAppMessageShareData({
                title: option.title,
                link: option.link,
                imgUrl: option.imgUrl,
                desc: option.desc,
                success() {
                },
                cancel() {
                  // 用户取消分享后执行的回调函数
                },
                fail() {
                  // alert("分享失败！"+JSON.stringify(s));
                }
              })
              wx.onMenuShareAppMessage({
                title: option.title,
                link: option.link,
                imgUrl: option.imgUrl,
                desc: option.desc,
                success() {
                },
                cancel() {
                  // 用户取消分享后执行的回调函数
                },
                fail() {
                  // alert("分享失败！"+JSON.stringify(s));
                }
              })
              // 微信分享到朋友圈
              wx.updateTimelineShareData({
                title: option.title,
                link: option.link,
                imgUrl: option.imgUrl,
                desc: option.desc,
                success() {
                },
                cancel() {
                },
                fail() {
                }
              })
              wx.onMenuShareTimeline({
                title: option.title,
                link: option.link,
                imgUrl: option.imgUrl,
                desc: option.desc,
                success() {
                },
                cancel() {
                },
                fail() {
                }
              })
              self.playAudio()
            })
          }
        });
      })

    },
    methods: {
      ...mapActions({
        getShareInfo: "fishH5/getShareInfo",
        // saveTicketInfo: "fishH5/saveTicketInfo",
        getTicketInfo: 'fishH5/getTicketInfo',
      }),

      goGet() {
        const token = window.localStorage.getItem('token')
        if (token) {
          this.getZhuli()
        }
        if (!token) {
          if (!this.code) {
            this.shouquan();
            return
          }
          this.getUserByCode()
        }
      },
      shouquan() {
        const index_url = 'http://zhiyang.yizehd.com/qiangpiao'
        window.location.href = 'http://zhiyang.yizehd.com/api/auth/auth?state=1&uri=' + index_url;
        window.localStorage.removeItem('token')
        return

      },
      getQueryString(name) {
        let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
          return unescape(r[2]);
        }
        return null;
      },
      show() {
      },
      getZhuli() {
        axios.get('/api/ticket/getQp', {
          params: {td: 88}
        }).then(() => {
          console.log('then')
        }).catch(() => {
          console.log('catch')
        })
      },
      playAudio() {
        const audio = this.$refs.myaudio
        audio.play();
        const buffer = 1.2;
        audio.addEventListener('timeupdate', function () {
          if (this.currentTime > this.duration - buffer) {
            this.currentTime = 0;
            this.play();
          }
        }, false);
      },
      getUserByCode() {
        axios.post('/api/auth/getUserByCode', {
          code: this.code,
          state: 1
        }).then((res) => {
          const token = res.data.description.token
          window.localStorage.setItem('token', token)
        }).catch(() => {
        })
      }
    }
  }
</script>
<style>
  .hiddenaudio {
    width: 1px;
    height: 1px;
    overflow: hidden;
    pointer-events: none;
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
  }
</style>
